package com.wewins.fota.controller;

import com.wewins.fota.bean.bo.filter.PkgFilterCond;
import com.wewins.fota.bean.dto.BatchReq;
import com.wewins.fota.bean.dto.PageReq;
import com.wewins.fota.bean.dto.PageRst;
import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.bean.po.PkgVersion;
import com.wewins.fota.common.constant.AuthConst;
import com.wewins.fota.service.PkgVersionService;
import com.wewins.fota.sys.token.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pkg")
public class PkgVersionController implements BasicController<PkgVersion, PkgFilterCond> {
    private final PkgVersionService service;

    private TokenManager tokenManager;

    @Autowired
    public PkgVersionController(PkgVersionService service, TokenManager tokenManager) {
        this.service = service;
        this.tokenManager = tokenManager;
    }

    @Override
    @PostMapping("/query")
    public ResponseRst<PageRst<PkgVersion>> queryAll(@RequestBody PageReq<PkgFilterCond> pageReq) {
        return ResponseRst.<PageRst<PkgVersion>>builder()
                .data(service.queryAll(pageReq))
                .build().withOk();
    }

    @Override
    @GetMapping("/query/{id}")
    public ResponseRst<PkgVersion> queryById(@PathVariable long id) {
        return ResponseRst.<PkgVersion>builder()
                .data(service.findById(id))
                .build().withOk();
    }

    @Override
    @PostMapping("/insert")
    public ResponseRst<PkgVersion> insert(HttpServletRequest request, @Validated @RequestBody PkgVersion inputParam) {
        int id = service.existPkg(inputParam, false);
        if (id != -1) {
            return ResponseRst.<PkgVersion>builder().build().withDefaultError("ERROR_PACKAGE_EXISTED");
        }
        return ResponseRst.<PkgVersion>builder()
                .data(service.insert(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), inputParam))
                .build().withOk();
    }

    @Override
    @DeleteMapping("/delete/{id}")
    public ResponseRst<Integer> deleteById(HttpServletRequest request, @PathVariable long id) {
        return ResponseRst.<Integer>builder()
                .data(service.deleteById(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), id))
                .build().withOk();
    }

    @Override
    @PutMapping("/update")
    public ResponseRst<Integer> update(HttpServletRequest request, @RequestBody PkgVersion inputParam) {
        int id = service.existPkg(inputParam, true);
        if (id != -1) {
            return ResponseRst.<Integer>builder().build().withDefaultError("ERROR_PACKAGE_UNIQUE_MODIFY_EXISTED");
        }
        return ResponseRst.<Integer>builder()
                .data(service.update(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), inputParam))
                .build().withOk();
    }

    @PutMapping("/update/batch")
    public ResponseRst<Integer> updateByBatch(HttpServletRequest request, @RequestBody BatchReq<PkgVersion> batchReq) {
        return ResponseRst.<Integer>builder()
                .data(service.update(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), batchReq.getBatch()))
                .build().withOk();
    }

    @PostMapping("/exist")
    public ResponseRst<Integer> existPkgByProductAndVersion(@RequestBody PkgVersion inputParam) {
        return ResponseRst.<Integer>builder()
                .data(service.existPkg(inputParam, false))
                .build().withOk();
    }
}
